<?php

/*
Upgrade Account (admin_upgrade.php)
*/

$page_title = "Upgrade Accounts";
$rank_check = 7;
include "../header.inc.php";

if (($username == "steve") AND ($game == "1"))
{

if (!$page)
{
	
ECHO <<<END

<form action=admin_upgrade.php?page=upgrade&game=$game method=post>
Upgrade Account: <input type=text name=upgrade_account><br>
In game ID: <input type=text name=gameID><Br>
<input type=submit value="Upgrade Account">
</form>

<form action=admin_upgrade.php?page=closegame&game=$game method=post>
Close game ID: <input type=text name=gameID><Br>
<input type=submit value="Close Game">
</form>

<form action=admin_upgrade.php?page=upgradegame&game=$game method=post>
Upgrade game ID: <input type=text name=gameID><Br>
to: <select name=package>
<option value=4>Ultra ($150)
<option value=3>Super ($100)
<option value=2>Basic ($75)
<option value=1>Small ($50)
<input type=submit value="Upgrade Game">
</form>

END;
}

if ($page == "upgradegame")
{
	$findGame = fetch("SELECT paid_until FROM game_tables WHERE id = '$gameID'");
	
	print "<form action=admin_upgrade.php?page=upgradegame2&game=$game&package=$package&gameID=$gameID method=post>
Paid Until: <input type=text name=paidtil value=$findGame[paid_until]><Br>
<input type=submit value=\"Upgrade Game\">
</form>";

}

if ($page == "upgradegame2")
{
	mysql_query("UPDATE game_tables SET package = '$package' WHERE id = '$gameID'");
	mysql_query("UPDATE game_tables SET paid_until = '$paidtil' WHERE id = '$gameID'");
	print "Game ID $gameID has been upgraded to package #$package until $paidtil.";
}

if ($page == "closegame")
{
	mysql_query("UPDATE game_tables SET status=1 WHERE id = '$gameID'");
	print "Game ID $gameID has been taken down.";
}

if ($page == "upgrade")
{
	$upgrade_account = strtolower(ereg_replace(" ", "", $upgrade_account));
	$findAccount = fetch("SELECT * FROM members2 WHERE username = '$upgrade_account' AND game = '$gameID'");
	mysql_query("UPDATE members2 SET premium = '1' WHERE username = '$findAccount[username]' AND game = '$gameID'");
	$findCredit = fetch("SELECT credit FROM game_tables WHERE id = '$gameID'");
	mysql_query("UPDATE game_tables SET credit=$findCredit[credit]+500 WHERE id = '$gameID'");
	$findLevel = fetch("SELECT * FROM members_stats WHERE game = '$gameID' AND user = '$findAccount[id]'");
	
	if (!$findLevel[id])
	{
		//Create Starting Stats
		
		$level = 1;
		$randStrength = rand(7,12);
		$randIntelligence = rand(7,12);
		$randSpeed = rand(7,12);
		$randDefense = rand(7,12);
		$randHP = rand(80,120);
		$randMag = rand(120,200);
		
		mysql_query("INSERT INTO members_stats (game, user, level, max_hp, cur_hp, max_magic, cur_magic, strength, defense, intelligence, speed) VALUES ('$gameID', '$findAccount[id]', '1', '$randHP', '$randHP', '$randMag', '$randMag', '$randStrength', '$randDefense', '$randIntelligence', '$randSpeed')");
	}
	
	$newLevel = $findLevel[level] + 2;
	$randStrength = $findLevel[strength] + rand(1,5);
	$randIntelligence = $findLevel[intelligence] + rand(1,5);
	$randSpeed = $findLevel[speed] + rand(1,5);
	$randDefense = $findLevel[defense] + rand(1,5);
	$randHP = $findLevel[max_hp] + rand(3,10);
	
	mysql_query("UPDATE members_stats SET level=$findLevel[level]+1 WHERE id = '$findLevel[id]' AND game = '$gameID'");
	mysql_query("UPDATE members_stats SET strength=$findLevel[strength]+$randStrength WHERE id = '$findLevel[id]' AND game = '$gameID'");
	mysql_query("UPDATE members_stats SET intelligence=$findLevel[intelligence]+$randIntelligence WHERE id = '$findLevel[id]' AND game = '$gameID'");
	mysql_query("UPDATE members_stats SET speed=$findLevel[speed]+$randSpeed WHERE id = '$findLevel[id]' AND game = '$gameID'");
	mysql_query("UPDATE members_stats SET defense=$findLevel[defense]+$randDefense WHERE id = '$findLevel[id]' AND game = '$gameID'");
	mysql_query("UPDATE members_stats SET max_hp=$findLevel[max_hp]+$randHP WHERE id = '$findLevel[id]' AND game = '$gameID'");
	mysql_query("UPDATE members_stats SET current_hp=$findLevel[current_hp]+$randHP WHERE id = '$findLevel[id]' AND game = '$gameID'");

	$findYourPet2 = mysql_query("SELECT * FROM user_pets2 WHERE owner = '$findAccount[id]' AND adoption = '0' AND game = '$gameID'");
	while ($findYourPet = mysql_fetch_array($findYourPet2))
	{
		$newLevel = $findYourPet[level] + 1;
		$randStrength = rand(0,2);
		$randIntelligence = rand(0,2);
		$randSpeed = rand(0,2);
		$randDefense = rand(0,2);
		$randHP = rand(0,3);
		
		mysql_query("UPDATE user_pets2 SET level=$findYourPet[level]+1 WHERE id = '$findYourPet[id]' AND game = '$gameID'");
		mysql_query("UPDATE user_pets2 SET strength=$findYourPet[strength]+$randStrength WHERE id = '$findYourPet[id]' AND game = '$gameID'");
		mysql_query("UPDATE user_pets2 SET intelligence=$findYourPet[intelligence]+$randIntelligence WHERE id = '$findYourPet[id]' AND game = '$gameID'");
		mysql_query("UPDATE user_pets2 SET speed=$findYourPet[speed]+$randSpeed WHERE id = '$findYourPet[id]' AND game = '$gameID'");
		mysql_query("UPDATE user_pets2 SET defense=$findYourPet[defense]+$randDefense WHERE id = '$findYourPet[id]' AND game = '$gameID'");
		mysql_query("UPDATE user_pets2 SET max_hp=$findYourPet[max_hp]+$randHP WHERE id = '$findYourPet[id]' AND game = '$gameID'");
		mysql_query("UPDATE user_pets2 SET current_hp=$findYourPet[current_hp]+$randHP WHERE id = '$findYourPet[id]' AND game = '$gameID'");
	}
	
	$findDiscount = fetch("SELECT * FROM discount_cards WHERE user = '$findAccount[id]' AND game = '$gameID'");
	$newRate = $findDiscount[discount] + .1;
	if ($findDiscount[id])
	{	
		mysql_query("UPDATE discount_cards SET discount = $newRate WHERE user = 	'$findAccount[id]' AND game = '$gameID'");
		mysql_query("UPDATE discount_cards SET reasons = '$findDiscount[reasons], Upgrading to Premium - 10%' WHERE user = '$findAccount[id]' AND game = '$gameID'");
	}
	if (!$findDiscount[id])
	{
		mysql_query("INSERT INTO discount_cards (game,user,discount,reasons) VALUES 	('$gameID','$findAccount[id]','$newRate',', Upgrading to Premium - 10%')");
	}
	
	$findInterest = fetch("SELECT * FROM bank2 WHERE user = '$findUsername[id]' AND game = '$gameID'");
	if (!$findInterest[rate]) { $findInterest[rate] = 1.07; }
	$interestRate2 = $findInterest[rate] + .02;
	if ($findInterest[id])
	{
		mysql_query("UPDATE bank2 SET rate = '$interestRate2' WHERE user = 	'$findAccount[id]' AND game = '$gameID'");
	}
	if (!$findInterest[id])
	{
		mysql_query("INSERT INTO bank2 (user,points,rate,game) VALUES 	('$findAccount[id]','100','$interestRate2','$gameID')");
	}
	
	$findTokens = fetch("SELECT * FROM upgrade_tokens WHERE user = '$findAccount[id]' AND game = '$gameID' AND upgrade = 'shop'");
	$newAmmount = $findTokens[ammount] + 10;
	if (!$findTokens[id])
	{
		mysql_query("INSERT INTO upgrade_tokens (user,game,upgrade,ammount) VALUES 	('$findAccount[id]','$gameID','shop','10')");
	}
	if ($findTokens[id])
	{
		mysql_query("UPDATE upgrade_tokens SET ammount=$newAmmount WHERE user = 	'$findAccount[id]' AND game = '$gameID' AND upgrade = 'shop'");
	}
	
	$findTokens = fetch("SELECT * FROM upgrade_tokens WHERE user = '$findAccount[id]' AND game = '$gameID' AND upgrade = 'gallery'");
	$newAmmount = $findTokens[ammount] + 10;
	if (!$findTokens[id])
	{
		mysql_query("INSERT INTO upgrade_tokens (user,game,upgrade,ammount) VALUES 	('$findAccount[id]','$gameID','gallery','10')");
	}
	if ($findTokens[id])
	{
		mysql_query("UPDATE upgrade_tokens SET ammount=$newAmmount WHERE user = 	'$findAccount[id]' AND game = '$gameID' AND upgrade = 'gallery'");
	}
	
	print "$upgrade_account has been upgraded in the game ID $gameID";
}
	
}

?>
